//
//  MessageRow.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/12.
//

import SwiftUI

struct MessageRow: View {
  let message: Message
  let isMe: Bool

  var body: some View {
    HStack(alignment: .top,spacing: 8) {
      if isMe {
        Spacer()
      } else {
        Avatar(icon: "driver_avatar")
      }

      if message.type == .text {
        TextMessage(isMe: isMe, text: message.text ?? "")
      } else if message.type == .image {
          ImageMessage(isMe: isMe, url: message.url!)
          .frame(width: 300)
      }else if message.type == .voice {
          VoiceMessage(isMe: isMe, url: message.url!)
      }

      if isMe {
        Avatar(icon: "passenger_chat_avatar")
      } else {
        Spacer()
      }
    }
    .padding(.init(top: 8, leading: 12, bottom: 8, trailing: 12))
   // .border(.blue)
  }

  struct Avatar: View {
    let icon: String

    var body: some View {
      Image(icon)
        .resizable()
        .frame(width: 40,height: 40)
    }
  }

  struct TextMessage: View {
    let isMe: Bool
    let text: String

    var body: some View {
      HStack(alignment: .top,spacing: 0) {
        if !isMe {
          Arrow(isMe: isMe)
            Text(text)
                .font(.system(size: 17))
                .padding(.horizontal,12)
                .padding(.vertical,10)
                .foregroundColor(.black)
                .background(background)
        }



        if isMe {
            Text(text)
                .font(.system(size: 17))
                .padding(.horizontal,12)
                .padding(.vertical,10)
                .background(background)
            Arrow(isMe: isMe)
        }
      }
    }

    private var background: some View {
      RoundedRectangle(cornerRadius: 4)
        .foregroundColor(Color("chat_\(isMe ? "me":"friend")_background"))
    }
  }


  struct Arrow: View {
    let isMe: Bool

    var body: some View {
      Path { path in
        path.move(to: .init(x: isMe ? 0 : 6, y: 14))
        path.addLine(to: .init(x: isMe ? 0 : 6, y: 26))
        path.addLine(to: .init(x: isMe ? 6 : 0, y: 26))
        path.addLine(to: .init(x: isMe ? 0 : 6, y: 14))
      }
      .fill(Color("chat_\(isMe ? "me" : "friend")_background"))
      .frame(width: 6,height:30)
    }
  }
}

struct MessageRow_Previews: PreviewProvider {
  static var previews: some View {
    MessageRow(message: Message.all[0], isMe: false)
  }
}
